package cn.mandifal.user.controller;

import cn.mandifal.pojo.DpSeller;
import cn.mandifal.pojo.DpUser;
import cn.mandifal.user.service.UserService;
import com.alibaba.dubbo.config.annotation.Reference;
import entity.Result;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户登陆注册控制层
 * @author shun
 */
@RestController
@RequestMapping("/userLoginController")
public class UserLoginController {
    @Reference(timeout = 3000)
    private UserService userService;
    /**
     * 显示用户名,如果有昵称则显示昵称
     * @return
     */
    @RequestMapping("/showName")
    public Map showName(){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        Map map=new HashMap();
        DpUser oneUser = userService.findOneUser(name);
        if (null==oneUser.getNickName()){
            map.put("loginName", name);
        }else{
            map.put("loginName",oneUser.getNickName());
        }
        if (null !=oneUser.getHeadPic()){
            //返回用户头像
            map.put("headPic",oneUser.getHeadPic());
        }
        return map;
    }
    /**
     * 修改用户名及密码
     * @return
     */
    @RequestMapping("/updatePassword")
    public Result updatePassword(@RequestBody Map map){
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        map.put("oldUsername",name);
        try {
            userService.updatePassword(map);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed();
        }
    }
    /**
     * 发送验证码
     * @param email 手机号
     * @return
     */
    @RequestMapping("/sendCode")
    public Result sendCode(String email){
        try {
            System.out.println(email);
            userService.createCode(email);
            return Result.success("验证码发送成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed("验证码发送失败");
        }
    }
    /**
     * 忘记密码
     */
    @RequestMapping("/forgetPassword")
    public Result forgetPassword(@RequestBody Map map){
        String username = (String) map.get("username");
        String email = (String) map.get("email");
        String emailCode = (String) map.get("emailCode");
        String password = (String) map.get("password");
        //如果查不到此商家返回错误
        DpUser dpUser = userService.findUserByUsernameAndEmail(username, email);
        if (dpUser==null){
            return Result.failed("用户与邮箱不匹配");
        }
        BCryptPasswordEncoder bCryptPasswordEncoder=new BCryptPasswordEncoder();
        //验证码匹配成功
        if (userService.checkCode(email, emailCode)){
            //设置密码
            dpUser.setPassword(DigestUtils.md5Hex(password));
            //更新商家密码
            userService.update(dpUser);
            return Result.success("密码修改成功");
        }
        return Result.failed();
    }


}
